//
//  BIDPropertyWorkConditionDetailHeaderView.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/10.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDPropertyWorkConditionDetailHeaderView.h"
#import "AppDelegate+StaticMethod.h"
#import "BIDPropertyWorkConditionDetailHeaderViewCell.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface BIDPropertyWorkConditionDetailHeaderView()<UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate>
{
    NSMutableArray *_dataSourceArr;
}
@end

@implementation BIDPropertyWorkConditionDetailHeaderView
@synthesize titleLabel;
@synthesize timeLabel;
@synthesize supportBtn;
@synthesize delegate;

- (void)awakeFromNib
{
    [super awakeFromNib];
    _dataSourceArr = [NSMutableArray array];
    _collectionView.allowsSelection = NO;
    [_collectionView registerClass:[BIDPropertyWorkConditionDetailHeaderViewCell class] forCellWithReuseIdentifier:@"identifier"];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [flowLayout setItemSize:CGSizeMake(CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
    [flowLayout setMinimumLineSpacing:0];
    [flowLayout setMinimumInteritemSpacing:0];
    [_collectionView setCollectionViewLayout:flowLayout];
    _collectionView.pagingEnabled = YES;
    _collectionView.bounces = NO;
}

- (void)prepareLayoutWithDic:(NSDictionary *)dic
{
    titleLabel.text = dic[@"title"];
    NSDate *date = [BIDGlobalMethods transToNSDateByFormatter:@"yyyy-MM-dd HH-mm-ss" fromStringDate:dic[@"createTime"]];
    timeLabel.text = [date formattedTime];
    [supportBtn setTitle:ANY2NSSTRING(dic[@"likeCount"]) forState:UIControlStateNormal];
    NSArray *arr = dic[@"listFile"];
    [_pageCtrl setNumberOfPages:arr.count];
    for(NSDictionary *subDic in dic[@"listFile"])
    {
        NSString *strFilePath = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], subDic[@"path"]];
        [_dataSourceArr addObject:strFilePath];
    }
    [_collectionView reloadData];
}

/**
 点赞
 */
- (IBAction)supportBtnHandler:(id)sender
{
    [delegate toSupport];
}

- (void)supportPlus
{
    NSString *strSupportCount = supportBtn.titleLabel.text;
    NSInteger supportCount = [strSupportCount integerValue] + 1;
    [supportBtn setTitle:[[NSString alloc] initWithFormat:@"%ld", supportCount] forState:UIControlStateNormal];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    NSLog(@"%ld", page);
    [_pageCtrl setCurrentPage:page];
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSourceArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = nil;
    NSUInteger row = indexPath.row;
    BIDPropertyWorkConditionDetailHeaderViewCell *headerCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    [headerCell.imgView setImageWithURL:[NSURL URLWithString:_dataSourceArr[row]] placeholderImage:[UIImage imageNamed:@"slide_placeholder.png"]];
    cell = headerCell;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
}

@end
